package com.baosight.sczx.common.zm.cInterface.cm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCM;
import com.baosight.sczx.zm.cm.domain.ZMCM2levelmaterialbase;

/**
 * 物资库存 错误信息需要自己书写
 * <p>Title: MaterialBase.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 杨鸣
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-17 下午03:14:52  杨鸣  Create
 *</pre>
 */
public class MaterialBase extends ServiceEPBase {
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static boolean hasUsedMaterial(Map<String,String> _argMap, EiInfo eiInfo) {
		
		List<Map>  _list = new ArrayList();
		
		try {
			_list = dao.query("ZMCMMaterialBase.queryHasUsedMaterial", _argMap);
			if(_list.size() < 1) {
				return false;
			}
			else {
				
			}
		} catch (Exception e) {
			
		}
		
		return true;
	}
	/**得到物资账面金额
	 * <p>Function getStockValue</p>
	 * @param inInfo
	 * @param string
	 * @param name
	 *  <p>History:
	 *    2011-6-27 20:13:04 李红星
	 * </p>
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static EiInfo getStockValue(EiInfo inInfo) {
		String fdMaterialInnerCode = inInfo.get("fdMaterialInnerCode").toString();//物资内码
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("fdMaterialInnerCode", fdMaterialInnerCode);
		List<Map> _list = new ArrayList<Map>();
		_list  = dao.query("ZMCMMaterialBase.querystockvalue",_map );
		String fdNum="0";
		String fdMonth="0";
		String fdNoinvoiceNum="0";
		String fdHasinvoiceNum="0";
		if(_list.size()>0){
		   fdNum = _list.get(0).get("fdNum").toString();
		   fdMonth = _list.get(0).get("fdMonth").toString();
		   fdNoinvoiceNum = _list.get(0).get("fdNoinvoiceNum").toString();
		   fdHasinvoiceNum = _list.get(0).get("fdHasinvoiceNum").toString();
		   
		}
		EiInfo outInfo=new EiInfo();
		outInfo.set("fdAccountSum",fdNum);
		outInfo.set("fdMonth", fdMonth);
		outInfo.set("fdNoInvoiceNum", fdNoinvoiceNum);
		outInfo.set("fdHasInvoiceNum", fdHasinvoiceNum);
		return outInfo;
	}

	/**根据盘点结果更新库存账
	 * <p>Function updateStockValue</p>
	 * @param inInfo
	 * @param string
	 * @param name
	 *  <p>History:
	 *    2011-6-29 11:02:10 李红星
	 * </p>
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static EiInfo updateStockValue(EiInfo inInfo) {
		Map<String, String> _map = new HashMap<String, String>();
		_map = (HashMap)inInfo.getBlock("result").getRows().get(0);
		dao.update("ZMCMMaterialBase.updatestockvalue",_map );
		//更新库存金额和库存实际金额
		dao.update("ZMCMMaterialBase.updatestocksum",_map);

		return inInfo;
	}

	/**根据调价结果更新库存账
	 * <p>Function updateValue</p>
	 * @param inInfo
	 * @param string
	 * @param name
	 *  <p>History:
	 *    2011-6-30 4:02:10 贾雪民
	 * </p>
	 */
	@SuppressWarnings({ "rawtypes" })
	public static EiInfo updateValue(EiInfo inInfo) {
		Map _map = (HashMap)inInfo.getBlock("result").getRows().get(0);
		if(SystemSettingCM.getInstance().getIsusePlanPrice().equals("1"))
		{
			dao.update("ZMCMMaterialBase.updatevalue",_map); //更新单价
			dao.update("ZMCMMaterialBase.updatesum",_map);	//更新库存金额
		}
		else
		{
			dao.update("ZMCMMaterialBase.updateActualvalue",_map); //更新实际单价
			dao.update("ZMCMMaterialBase.updateActualsum",_map);	//更新实际库存金额
		}
		return inInfo;
	}
	
	/**
	 * 结转时修改库存所有数据的结转年月
	 * <p>Function: UpdateYearMonthForHis</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28下午04:25:56 杨鸣
	 * </p>
	 *
	 */
		public static boolean UpdateYearMonthForHis(String fdYear,String fdMonth){ 
			Map<String, String> _args = new HashMap<String, String>();
			_args.put("fdYear", fdYear);
			_args.put("fdMonth", fdMonth);

			try {
				 dao.insert("ZMCMMaterialBase.updateYearMonthForHis", _args);

			} catch (Exception e) {
				return false;
				
			}
			return true;
		}
		
		/** 取一级库存的物资信息  供二级库存管理模块适用 
		 * <p>Function updateValue</p>
		 * @param inInfo
		 * @param string
		 * @param name
		 *  <p>History:
		 *    2011-7-11 4:02:10 张坤
		 * </p>
		 */
		public  EiInfo select1LevelMaterial(EiInfo inInfo) {
			EiInfo outInfo = super.query(inInfo, "ZMCMMaterialBase.queryMaterialcode",new ZMCM2levelmaterialbase());
			return outInfo;
		}	
		
		/**
		 * 
		 * <p>Function: isInAccountDate</p>
		 * <p>Description: 是否在记账日期内</p>
		 * <p>return value:int -2:之前有未结转库存</p>
		 * <p>                 -1：库存未结转，但日期不在记账范围内</p>
		 * <p>                  0：库存已结转，不允许记账</p>
		 * <p>                  1：正常范围内，允许记账</p>
		 * <p>throws Exception:</p>
		 * <p>History:
		 *    2012-3-8下午01:14:31 wangkun
		 * </p>
		 *
		 */
		@SuppressWarnings("rawtypes")
		public static int isInAccountDate(String date)
		{
			int accountDay = SystemSettingCM.getInstance().getAutoBaseDate().intValue();
			int currentYear = Integer.parseInt(date.split("-")[0]);
			int currentMonth = Integer.parseInt(date.split("-")[1]);
			int currentDay = Integer.parseInt(date.split("-")[2]);
			int baseMonth = 0;
			int baseYear = 1990;
			int accountMonth = -2;
			int accountYear = 1999;
			
			List _list = dao.query("ZMCMMaterialBaseHis.autoBaseMonth", new HashMap());
			if(_list.size() > 0)
			{
				if(((Map)_list.get(0)).get("fdMonth") != null)
				{
					baseMonth = Integer.parseInt(((Map)_list.get(0)).get("fdMonth").toString());
					baseYear = currentYear;
					accountMonth = currentMonth;
					accountYear = currentYear;
				}
			}
			
			//如果记录日期超出记账日期，则月加1
			if(accountMonth != -2 && accountDay < currentDay)
			{
				if(accountMonth == 12)
				{
					accountMonth = 13;
					accountYear = accountYear + 1;
				}
				else
				{
					accountMonth = accountMonth + 1;
				}
			}
			
			if(baseYear < accountYear && currentMonth == 1 && accountDay > currentDay)
			{
				return 1;
			}
			
			if(-1 > (accountMonth-baseMonth) || (accountMonth-baseMonth) > 1)
			{
				return -2;
			}
			
			if(accountYear > currentYear)
			{
				return 1;
			}
			
			if(accountYear == currentYear)
			{
				if(accountMonth > baseMonth)
				{
					if(baseMonth == currentMonth && accountDay > currentDay)
					{
						return -1;
					}
					else
					{
						return 1;
					}
				}
				else if(accountMonth == baseMonth)
				{
					return 0;
				}
			}
			
			return 0;
		}
		
		/**
		 * 
		 * <p>Function: getAccountDate</p>
		 * <p>Description: 获得记账年月</p>
		 * <p>return value:int[]</p>
		 * <p>History:
		 *    2012-3-8下午04:46:30 wangkun
		 * </p>
		 *
		 */
		@SuppressWarnings("rawtypes")
		public static int[] getAccountDate(String date)
		{
			int accountDay = SystemSettingCM.getInstance().getAutoBaseDate().intValue();
			int currentYear = Integer.parseInt(date.split("-")[0]);
			int currentMonth = Integer.parseInt(date.split("-")[1]);
			int currentDay = Integer.parseInt(date.split("-")[2]);
			int accountMonth = currentMonth;
			int accountYear = currentYear;
			int baseMonth = 1;
			
			//如果记录日期超出记账日期，则月加1
			if(accountDay < currentDay)
			{
				if(accountMonth == 12)
				{
					accountMonth = 1;
					accountYear = accountYear + 1;
				}
				else
				{
					accountMonth = accountMonth + 1;
				}
			}
			
			List _list = dao.query("ZMCMMaterialBaseHis.autoBaseMonth", new HashMap());
			if(_list.size() > 0)
			{
				if(((Map)_list.get(0)).get("fdMonth") != null)
				{
					baseMonth = Integer.parseInt(((Map)_list.get(0)).get("fdMonth").toString());
				}
			}
			
			int[] result = {accountYear, accountMonth, baseMonth};
			return result;
		}
}
